import React, { useState, useCallback } from 'react';
import { Modal, Button, Icon, message } from 'antd';

import { isSuccess } from '@/utils/request';
import { hrRecheck } from '@/services/salaryManage/salaryCalculate';

const { updateData } = hrRecheck;

/**
 *薪资单优异，开启薪资单重新生成弹窗
 * @param checkList
 * @param actionType
 */
const useArticleWage = (callback, postData) => {
    //  弹窗=》确定按钮是否加载状态
    const [confirmLoad, setConfirmLoad] = useState(false);
    const [modalVisible, setModalVisible] = useState(false);
    const [msg, setMsg] = useState('');
    const [power, setPower] = useState(false);

    const onRejectClose = () => {
        setModalVisible(false)
    }

    // 点击确认
    const onRejectOk = useCallback(async () => {
        setConfirmLoad(true)
        const rejectRes = await updateData({
            ...postData
        })

        setConfirmLoad(false)

        if (isSuccess(rejectRes)) {
            setModalVisible(false)
            // eslint-disable-next-line no-unused-expressions
            callback && callback();
        } else {
            message.error(rejectRes.msg)
        }
    }, [callback, postData])

    // 开启弹窗
    const openArticleModal = (articleWagesMsg, articleWagesPower) => {
        setMsg(articleWagesMsg)
        setPower(articleWagesPower)
        setModalVisible(true)
    }

    return {
        openArticleModal,
        render() {
            return modalVisible ? <Modal
                title="提示"
                visible={modalVisible}
                onOk={onRejectOk}
                cancelText={null}
                onCancel={onRejectClose}
                destroyOnClose
                centered
                confirmLoading={confirmLoad}
                footer={
                power
                    ? [
                        <Button key="back" onClick={onRejectClose}>
                            取消生成
                        </Button>,
                        <Button key="submit" type="primary" loading={confirmLoad} onClick={onRejectOk}>
                            重新生成
                        </Button>
                      ]
                    : [
                        <Button key="back" type="primary" onClick={onRejectClose}>
                            知道了
                        </Button>
                      ]
            }
        >
                <Icon type="exclamation-circle" style={{ fontSize: 16, color: '#FFA22D', marginRight: 5 }} />
                {msg}
            </Modal> : null
        }
    }
};

export default useArticleWage;
